﻿/**
* QSmart Andon
*
* 2012-10-11
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.SystemManage
{
    public class DeptService : DbHelperBase
    {
        /// <summary>
        /// 获取部门绑定下拉列表
        /// </summary>
        /// <returns></returns>
        public List<DepartmentEntity> GetDeptList()
        {
            var list = new List<DepartmentEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append("SELECT DptID,DptName FROM UT_Department");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new DepartmentEntity();
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddDept(DepartmentEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO UT_Department(DptID,DptName,DptDesc) ");
            strSql.Append(" VALUES(@DptID,@DptName,@DptDesc) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@DptID", model.DptID);
                    Db.AddInParameter(cmd, "@DptName", model.DptName);
                    Db.AddInParameter(cmd, "@DptDesc", model.DptDesc);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ModifyDept(DepartmentEntity model)
        {

            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_Department SET [DptName] = @DptName,[DptDesc] = @DptDesc WHERE DptID = @DptID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@DptName", model.DptName);
                    Db.AddInParameter(cmd, "@DptDesc", model.DptDesc);
                    Db.AddInParameter(cmd, "@DptID", model.DptID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 获取部门单条信息
        /// </summary>
        /// <param name="dptId"></param>
        /// <returns></returns>
        public DepartmentEntity GetDeptInfo(string dptId)
        {
            var model = new DepartmentEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT DptID, DptName, DptDesc FROM UT_Department Where DptID=@DptID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                Db.AddInParameter(cmd, "@DptID", dptId);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            model.DptDesc = reader["DptDesc"] == obj ? null : Convert.ToString(reader["DptDesc"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="dptId"></param>
        /// <returns></returns>
        public bool Remove(string dptId)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_Department Where DptID=@DptID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@DptID", dptId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public int GetDeptCount(DepartmentQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_Department AS A WHERE 1=1 ");

            if (!string.IsNullOrEmpty(qcmodel.DptName))
            {
                strSql.Append(" AND DptName like '%" + qcmodel.DptName + "%'");
            }

            var count = 0;

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        /// <summary>
        /// 获取部门列表分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public List<DepartmentEntity> GetDeptList(DepartmentQcEntity qcmodel)
        {
            var list = new List<DepartmentEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,DptID,DptName,DptDesc FROM UT_Department ");
            strSql.Append(" WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.DptName))
            {
                strSql.Append(" AND DptName like '%" + qcmodel.DptName + "%'");
            }
            strSql.Append(") AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new DepartmentEntity();
                            if (reader["DptID"] != DBNull.Value)
                                model.DptID = new Guid(reader["DptID"].ToString());
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            model.DptDesc = reader["DptDesc"] == obj ? null : Convert.ToString(reader["DptDesc"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
